package com.zw.pdm.module.est.service;
import com.zw.pdm.entity.designtask.DsnProStruct;
import com.zw.pdm.entity.sys.User;
import com.zw.pdm.module.est.mapper.SolderingMapper;
import com.zw.pdm.module.est.model.InputCostsModel;
import com.zw.pdm.module.est.model.SolderingCheckModel;
import com.zw.pdm.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Description: 服务层
 * @Author: China.kxx
 * @Date: 2020/11/04 20:40
 */
@Service
public class InputCostsService {
    @Autowired
    private SolderingMapper solderingMapper;
    /**
     * 查询
     * @param inputCostsModel
     * @return 工时列表数据
     */
    public List<InputCostsModel> inputCuttingTime (InputCostsModel inputCostsModel) {

        List<InputCostsModel> cutting = solderingMapper.selectcuttingTime(inputCostsModel);
        return cutting;

    }
    /**
     * 查询
     * @param inputMaterialModel
     * @return 辅材列表数据
     */
    public List<InputCostsModel> inputCuttingMaterial (InputCostsModel inputMaterialModel) {

        List<InputCostsModel> Material = solderingMapper.selectcuttingMaterial(inputMaterialModel);
        return Material;

    }
    /**
     * 保存人工、辅材信息
     * @param manpower
     * @return
     */
    public int saveManpowerInfo(InputCostsModel manpower) {
        User user = ShiroUtils.getUser();
        if (manpower.getMName() == 1) {
            if (manpower.getId() == null){
                manpower.setManInputUser(user.getName());
                manpower.setManEditDate(new Date());
//                manpower.setFkId(manpower.getKbId());
//                manpower.setFkCode(manpower.getCode());
//                manpower.setFkInputId(manpower.getInputId());
                if(manpower.getSpecialId() == 60) {
                    manpower.setDesignType(1);
                } else if(manpower.getSpecialId() == 61) {
                    manpower.setDesignType(3);
                } else if(manpower.getSpecialId() == 62) {
                    manpower.setDesignType(2);
                }
                manpower.setManValidation(0);
                manpower.setManDeleteFlag(0);
                manpower.setManReceiveFlag(0);
                manpower.setFkInputId(2);
                manpower.setManStatus(0);
                manpower.setCostKind("铆焊");
                solderingMapper.insertManpower(manpower);
            } else {
                solderingMapper.updateManpower(manpower);
            }
            return manpower.getMName();
        } else {
            if (manpower.getMid() == null) {
                manpower.setMatInputUser(user.getName());
                manpower.setMatEditDate(new Date());
//                manpower.setMfkId(manpower.getKbId());
//                manpower.setFkCode(manpower.getCode());
//                manpower.setMfkInputId(manpower.getInputId());
                if(manpower.getSpecialId() == 60) {
                    manpower.setDesignType(1);
                } else if(manpower.getSpecialId() == 61) {
                    manpower.setDesignType(3);
                } else if(manpower.getSpecialId() == 62) {
                    manpower.setDesignType(2);
                }
                manpower.setMatValidation(0);
                manpower.setMatDeleteFlag(0);
                manpower.setMatReceiveFlag(0);
                manpower.setFkInputId(2);
                manpower.setMatStatus(0);
                manpower.setCostKind("铆焊");
                solderingMapper.insertMaterial(manpower);
            } else {
                solderingMapper.updateMaterial(manpower);
            }
            return manpower.getMName();
        }
    }
    /**
     * 查询一条工时数据的方法
     * @param id
     * @return
     */
    public InputCostsModel selectOneManpowerTime(Integer id){
        return solderingMapper.selectOneManpowerTime(id);
    }
    /**
     * 查询一条辅材数据的方法
     * @param mid
     * @return
     */
    public InputCostsModel selectOneMaterial(Integer mid){
        return solderingMapper.selectOneMaterial(mid);
    }



    /**
     * 删除一条工时数据验证方法
     * @param manId
     * @return InputCostsModel
     */
    public int delManHour(Integer manId) {
        List<InputCostsModel> inputCostsModels = solderingMapper.selectCost(manId);
        if (inputCostsModels.size() > 0){
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 删除一条辅材数据验证方法
     * @param mid
     * @return InputCostsModel
     */
    public int delMaterial(Integer mid) {
        List<InputCostsModel> inputCostsModels = solderingMapper.selectMaterial(mid);
        if (inputCostsModels.size() > 0){
            return 1;
        } else {
            return 0;
        }
    }
    /**
     * 删除一条工时数据的方法
     * @param manId
     * @return InputCostsModel
     */
    public int deleteManHour(Integer manId) {
        return solderingMapper.deleteManHour(manId);
    }
    /**
     * 删除一条辅材数据的方法
     * @param mid
     * @return
     */
    public int deleteMaterial(Integer mid) {
        return solderingMapper.deleteMaterial(mid);
    }
    /**
     * 工时清空（全部）逻辑验证
     * @param inputCostsModel
     * @return InputCostsModel
     */
    public int delManHourAll(InputCostsModel inputCostsModel) {
        List<InputCostsModel> inputCostsModel1 = solderingMapper.selectManHourAll1(inputCostsModel);
        List<InputCostsModel> inputCostsModel2 = solderingMapper.selectManHourAll2(inputCostsModel);
        if (inputCostsModel1.size() == 0 && inputCostsModel2.size() > 0){
            return -1;
        }else if (inputCostsModel1.size() == 0 && inputCostsModel2.size() == 0){
            return 0;
        }else if(inputCostsModel1.size() < inputCostsModel2.size()){
            return -1;
        }else if(inputCostsModel1.size() == inputCostsModel2.size()){
            return 1;
        }else {
            return 0;
        }
    }
    /**
     * 清空对应工时明细
     * @param inputCostsModel
     * @return InputCostsModel
     */
    public int deleteManHourAll(InputCostsModel inputCostsModel) {
        return solderingMapper.deleteManHourAll(inputCostsModel);
    }
    /**
     * 辅材清空（全部）逻辑验证
     * @param inputCostsModel
     * @return InputCostsModel
     */
    public int delMatAll(InputCostsModel inputCostsModel) {
        List<InputCostsModel> inputCostsModel1 = solderingMapper.selectMatAll1(inputCostsModel);
        List<InputCostsModel> inputCostsModel2 = solderingMapper.selectMatAll2(inputCostsModel);
        if (inputCostsModel1.size() == 0 && inputCostsModel2.size() > 0){
            return -1;
        }else if (inputCostsModel1.size() == 0 && inputCostsModel2.size() == 0){
            return 0;
        }else if(inputCostsModel1.size() < inputCostsModel2.size()){
            return -1;
        }else if(inputCostsModel1.size() == inputCostsModel2.size()){
            return 1;
        }else {
            return 0;
        }
    }
    /**
     * 清空对应辅材明细
     * @param inputCostsModel
     * @return InputCostsModel
     */
    public int deleteMatAll(InputCostsModel inputCostsModel) {
        return solderingMapper.deleteMatAll(inputCostsModel);
    }

    /**
     * 编辑数据验证方法
     * @param manpower
     * @return InputCostsModel
     */
    public int selectVerify(InputCostsModel manpower) {
        List<InputCostsModel> inputCostsModelsMan = solderingMapper.selectVerifyMan(manpower);
        List<InputCostsModel> inputCostsModelsMat = solderingMapper.selectVerifyMat(manpower);
        if (manpower.getMName() == 1) {
            if (inputCostsModelsMan.size() > 0) {
                return 1;
            } else {
                return 0;
            }
        } else if (manpower.getMName() == 2) {
            if (inputCostsModelsMat.size() > 0) {
                return 1;
            } else {
                return 0;
            }
        }
        return 2;
    }
    @Transactional(rollbackFor = Exception.class)
    public int solderingVerify(InputCostsModel inputCostsModel) {
        int resCount = solderingMapper.updatetMatVerify(inputCostsModel);
        resCount += solderingMapper.updatetManVerify(inputCostsModel);
        return resCount;
    }
    /** 根据项目号查询部分 */
    public List<DsnProStruct> findPartByProjectCode(String code, String type) {
        return solderingMapper.findPartByProjectCode(code, type);
    }

    /**
     * 查询
     * @param solderingCheckModel
     * @return 审核列表
     */
    public List<SolderingCheckModel> solderingCheck (SolderingCheckModel solderingCheckModel) {
        List<SolderingCheckModel> solderingCheck = solderingMapper.selectSolderingCheck(solderingCheckModel);
        return solderingCheck;
    }

    public SolderingCheckModel information(String fkCode, String partName, Integer specialId) {
        return solderingMapper.selectProject(fkCode,partName,specialId);
    }

    public List<InputCostsModel> solderManpower(InputCostsModel inputCostsModel) {
        return solderingMapper.selectMapower(inputCostsModel);
    }

    public List<InputCostsModel> solderMaterialVerify(InputCostsModel inputCostsModel) {
        return solderingMapper.selectMaterialVerify(inputCostsModel);
    }
}